package com.ruoyi.project.test;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.fit.pdfdom.PDFDomTree;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;

public class PdfRed {

    public static final String PDF_SUFFIX = ".pdf";
    public static final String HTML_SUFFIX = ".html";

    public static void main(String[] args) throws Exception {

        List<Integer> arr = new ArrayList<>(); // 关闭的箱子编号数组 未判断是否使用
        arr.add(6);
        arr.add(4);
        arr.add(5);
        arr.add(3);
        arr.add(2);
        arr.add(1);
        arr.add(7);

        Collections.sort(arr);

        for(Integer a :arr){
            System.out.println(a);
        }

        //pdf转换html
//        String path = "D:\\项目开发\\2024.11\\2024.11.06_pdf提取红色文字\\1.pdf";
//        pdfToHtml(path);

        //提取html红色文字
        // 替换为你的HTML文件路径
//        String filePath = "D:\\项目开发\\2024.11\\2024.11.06_pdf提取红色文字\\1.html";
//
//        try {
//            // 解析HTML文件
//            Document doc = Jsoup.parse(new File(filePath), "UTF-8");
//            Elements elements = doc.select("*"); // 选择所有元素
//
//            for (Element element : elements) {
//                // 获取元素的内联样式
//                String style = element.attr("style");
//                // 使用正则表达式匹配颜色值
//                if (style != null && containsSpecificColor(style)) {
//                    System.out.println(element.text());
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    // 检查样式字符串是否包含特定颜色
    private static boolean containsSpecificColor(String style) {
        Pattern pattern = Pattern.compile("color:\\s*#ff3000", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(style);
        return matcher.find();
    }

    /**
     * 将指定路径的pdf转成Html
     * @param path 文件路径
     */
    public static void pdfToHtml(String path) throws Exception {
        if (path == null || path.length() == 0) {
            throw new Exception("path为空！");
        }
        if (!path.endsWith(PDF_SUFFIX)) {
            throw new Exception("文件类型不是pdf，请检查文件类型！");
        }
        int endIndex = path.indexOf(PDF_SUFFIX);
        String pathPrefixWithFileName = path.substring(0, endIndex);
        String outputPath = pathPrefixWithFileName + HTML_SUFFIX;
        byte[] bytes = getBytes(path);
        pdfBytesToHtml(bytes,outputPath);
    }

    /**
     * 将pdf的字节数组转成html
     * @param bytes 字节数组
     * @param outputPath html输出路径
     */
    public static void pdfBytesToHtml(byte[] bytes,String outputPath){
        /**
         * try() 写在()里面会自动关闭流
         */
        try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outputPath)), "UTF-8"));) {
            /**
             * 加载PDF文档
             */
            PDDocument document = PDDocument.load(bytes);
            PDFDomTree pdfDomTree = new PDFDomTree();
            pdfDomTree.writeText(document, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将文件转换为byte数组
     * @param filePath
     * @return byte[]
     */
    private static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

}
